<?php

namespace Home\Controller;

use Think\Controller;

class CwController extends Controller{

    public function addworkorder(){
        $param=I('param.');
        $title = $param['title'];
        $content = $param['content'];
        $imgurl =$param['imgurl'];
        $accept_apart_id = $param['accept_apart_id'];
        $user_name = $param['user_name'];
        $phone = $param['phone'];
        if (!empty($phone)) {
            $user_vid = M('user', '', 'USER')->where(['mobile' => $phone, 'truename' => $user_name])->getfield('vid');
            if (empty($user_vid)) {
                $this->ajaxReturn(array('state' => '2', 'result' => '请添加正确的手机号和用户名'), 'json');
            }
        }
        $apart_id =$param['apart_id'];
        $publish_name = $param['name'];
        $accept_name = $param['accept_name'];
        M('work_order', '', 'KEFU')->startTrans();
        M('work_order_detail', '', 'KEFU')->startTrans();
        $arr = array('level' => I('level'), 'apart_id' => $apart_id, 'publish_name' => $publish_name, 'accept_apart_id' => $accept_apart_id, 'accept_name' => $accept_name, 'title' => $title, 'user_name' => $user_name, 'user_phone' => $phone, 'ctime' => time(), 'upd_time' => time());
        $add = M('work_order', '', 'KEFU')->add($arr);
        $arr = array('content' => $content, 'img' => $imgurl, 'wo_id' => $add);
        $add1 = M('work_order_detail', '', 'KEFU')->add($arr);
        if ($add && $add1) {
            M('work_order', '', 'KEFU')->commit();
            M('work_order_detail', '', 'KEFU')->commit();
            $this->ajaxreturn(['state'=>'1','result'=>'添加成功']);
        } else {
            M('work_order', '', 'KEFU')->rollback();
            M('work_order_detail', '', 'KEFU')->rollback();
            $this->ajaxreturn(['state'=>'2','result'=>'添加失败']);
        }
    }

    /**
     * 工单详情 回复
     */
    public function workorder_feedback(){

        $id = I('id');
        $content = I('back_content');
        $img = I('img');
        $arr = array('w_id' => $id, 'apart_id' => 4, 'name' => $_SESSION['m_truename'], 'content' => $content, 'img' => $img, 'ctime' => time());
        $wo=M('work_order', '', 'KEFU')->where(['id' => $id])->field('apart_id,publish_name,status')->find();
        M('work_order_answer', '', 'KEFU')->startTrans();
        M('work_order', '', 'KEFU')->startTrans();
        $info = M('work_order_answer', '', 'KEFU')->add($arr);
        if ($info) {
            if( $wo['publish_name']!=$_SESSION['m_truename']&&$wo['status']==1){
                $data['status']=2;
            }
            $data['upd_time']=time();
            if(! M('work_order', '', 'KEFU')->where(['id' => $id])->save($data)){//工单更新时间
                M('work_order_answer', '', 'KEFU')->rollback();
                M('work_order', '', 'KEFU')->rollback();
            }
            $str = array('admin_id' => $_SESSION['m_id'], 'content' => '回复工单' . $id . '记录为' . $info, 'ctime' => time());
            action_log($str);
            M('work_order_answer', '', 'KEFU')->commit();
            M('work_order', '', 'KEFU')->commit();
            $this->ajaxReturn(['code' => '1001', 'result' => '回复成功']);
            die;
        }
        M('work_order_answer', '', 'KEFU')->rollback();
        M('work_order', '', 'KEFU')->rollback();
        $this->ajaxReturn(['code' => '1002', 'result' => '回复失败']);
    }
    /**
     * 转交工单
     */
    public function workorder_turn(){
        if (!IS_POST) {
            $this->ajaxReturn(['status' => '3', 'result' => '操作异常'], 'json');
        }
        M('work_order_log','','KEFU')->startTrans();
        M('work_order','','KEFU')->startTrans();
        $arr=array();
        $data=I('post.');
        $data['accept_apart_id']=$data['apart'];
        unset($data['apart']);

        $order=M('work_order','','KEFU')->where(['id'=>I('work_id')])->field('title,apart_id,publish_name,accept_apart_id,accept_name')->find();
        $adminwhere['status']=1;
        $data['accept_name']=I('accept_name');
        $data['apart_id']=4;
        $data['change_name']=$_SESSION['m_truename'];
        $data['ctime']=time();
        if(M('work_order_log','','KEFU')->add($data)){
            $orderdata['upd_time']=time();
            $orderdata['accept_apart_id']=I('apart');
            $orderdata['accept_name']=$data['accept_name'];
            if(M('work_order','','KEFU')->where(['id'=>I('work_id')])->save($orderdata)){
                M('work_order_log','','KEFU')->commit();
                M('work_order','','KEFU')->commit();
                $str = array('admin_id' => $_SESSION['m_id'], 'content' => '转交工单' . I('work_id'), 'ctime' => time());
                action_log($str);
                $this->ajaxReturn(['code' => '1001', 'result' => '转交成功']);
            }else{
                M('work_order_log','','KEFU')->rollback();
                M('work_order','','KEFU')->rollback();
                $this->ajaxReturn(['code' => '1002', 'result' => '转交失败']);
            }
        }else{
            M('work_order_log','','KEFU')->rollback();
            M('work_order','','KEFU')->rollback();
            $this->ajaxReturn(['code' => '1002', 'result' => '转交失败','err'=>$data]);
        }
    }
    /**
     * 完结工单
     */
    public function workorder_finish()
    {
        $id = I('id');
        $info = M('work_order', '', 'KEFU')->where(['id' => $id])->setfield('status', '3');
        if ($info) {
            $str = array('admin_id' => $_SESSION['m_id'], 'content' => '完结工单' . $id, 'ctime' => time());
            action_log($str);
            $update = M('work_order', '', 'KEFU')->where(['id' => $id])->setfield('upd_time', time());//工单更新时间
            $this->ajaxReturn(['code' => '1001', 'result' => '完成']);
            die;
        }
        $this->ajaxReturn(['code' => '1002', 'result' => '失败']);

    }
    /**
     * 关闭工单
     */
    public function close(){
        if(M('work_order', '', 'KEFU')->where(['id'=>I('id')])->save(['status'=>4])){
            $str = array('admin_id' => $_SESSION['m_id'], 'content' => '工单' . I('id').'关闭', 'ctime' => time());
            action_log($str);
            $this->ajaxreturn(['code'=>1001,'result'=>'关闭工单成功'],'json');
        }else{
            $this->ajaxreturn(['code'=>1002,'result'=>'关闭工单失败'],'json');
        }
    }
    /**
     * 补充说明
     */
    public function extra_content(){
        $id = I('id');
        $data['extra_content']=I('extra_content');
        if(M('work_order_detail', '', 'KEFU')->where(['wo_id'=>$id])->save($data)){
            $str = array('admin_id' => $_SESSION['m_id'], 'content' => '工单' . $id.'修改补充说明', 'ctime' => time());
            action_log($str);
            $this->ajaxreturn(['code'=>1001,'result'=>'工单补充说明编辑成功'],'json');
        }else{
            $this->ajaxreturn(['code'=>1002,'result'=>'工单补充说明编辑失败'],'json');
        }
    }
}